#!/bin/bash
# installation JDK

# 官网 www.oracle.com 
# 下载 https://repo.huaweicloud.com:8443/artifactory/java-local/jdk/

. ./public_functions &>/dev/null #公共函数库
. /tmp/public_functions &>/dev/null #公共函数库

##1 二进制安装jdk
jdk_Install_centos7() {

  source /etc/profile && java -version 2>/dev/null
  if [[ $? -eq 0 ]]; then
    logTips "jdk maybe already exist" "(jdk可能已安装,请手动检查)" #1-20#
    exit
  fi

#  readonly jdk_Download_address="https://repo.huaweicloud.com/java/jdk/8u202-b08/jdk-8u202-linux-x64.tar.gz"  # jdk下载地址
#  readonly jdk_Package="jdk-8u202-linux-x64.tar.gz"	# jdk压缩包名称
#  readonly jdk_Catalog="jdk1.8.0_202"		        # 压缩包解压后的目录名称

  readonly jdk_Download_address="https://repo.huaweicloud.com:8443/artifactory/java-local/jdk/11+28/jdk-11_linux-x64_bin.tar.gz"  # jdk下载地址
  readonly jdk_Package="jdk-11_linux-x64_bin.tar.gz"	# jdk压缩包名称
  readonly jdk_Catalog="jdk-11"		                # 压缩包解压后的目录名称

  logW "1. 开始下载 $jdk_Package" #1-20#
  logY "curl -# --output /tmp/$jdk_Package $jdk_Download_address" #1-20#
    if [[ ! -f /tmp/$jdk_Package ]]; then
      curl -# --output /tmp/$jdk_Package $jdk_Download_address
      checkTF #21#
    else
      logTips "" "$jdk_Package Already exists ($jdk_Package 安装包已存在)"
    fi

  logW "2. 开始解压 $jdk_Package" #1-20#
  logY "tar -xzf /tmp/$jdk_Package -C /usr/local/" #1-20#
    tar -xzf /tmp/$jdk_Package -C /usr/local/
  checkTF #21#

  logW "3. 为jdk目录 创建软连接" #1-20#
  logY "ln -s /usr/local/$jdk_Catalog /usr/local/jdk" #1-20#
    ln -s /usr/local/$jdk_Catalog /usr/local/jdk
  checkTF #21#

  logW "4. 修改环境变量" #1-20#

    var1="export JAVA_HOME=/usr/local/$jdk_Catalog"
    var2='export PATH=$JAVA_HOME/bin:$PATH'
    var3='export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar'

    for i in "$var1" "$var2" "$var3"
    do
       logY "grep --quiet \"^$i$\" /etc/profile || sed -i \"\$a $i\" /etc/profile" #1-20#
       grep --quiet "^$i$" /etc/profile || sed -i "\$a $i" /etc/profile
       checkTF #21#
    done

  logY "java -version" #1-20#
    source /etc/profile && java -version
  checkTF

}

##2 二进制安装jdk
jdk_Install_ubuntu() {

  source /etc/profile && java -version 2>/dev/null
  if [[ $? -eq 0 ]]; then
    logTips "jdk maybe already exist" "(jdk可能已安装,请手动检查)" #1-20#
    exit
  fi

#  readonly jdk_Download_address="https://repo.huaweicloud.com/java/jdk/8u202-b08/jdk-8u202-linux-x64.tar.gz"  # jdk下载地址
#  readonly jdk_Package="jdk-8u202-linux-x64.tar.gz"	# jdk压缩包名称
#  readonly jdk_Catalog="jdk1.8.0_202"		        # 压缩包解压后的目录名称

  readonly jdk_Download_address="https://repo.huaweicloud.com:8443/artifactory/java-local/jdk/11+28/jdk-11_linux-x64_bin.tar.gz"  # jdk下载地址
  readonly jdk_Package="jdk-11_linux-x64_bin.tar.gz"	# jdk压缩包名称
  readonly jdk_Catalog="jdk-11"		                # 压缩包解压后的目录名称


  logW "1. 开始下载 $jdk_Package" #1-20#
  logY "sudo curl -# --output /tmp/$jdk_Package $jdk_Download_address" #1-20#
    if [[ ! -f /tmp/$jdk_Package ]]; then
      sudo curl -# --output /tmp/$jdk_Package $jdk_Download_address
      checkTF #21#
    else
      logTips "" "$jdk_Package Already exists ($jdk_Package 安装包已存在)"
    fi

  logW "2. 开始解压 $jdk_Package" #1-20#
  logY "sudo tar -xzf /tmp/$jdk_Package -C /usr/local/" #1-20#
    sudo tar -xzf /tmp/$jdk_Package -C /usr/local/
  checkTF #21#

  logW "3. 为jdk 创建软连接" #1-20#
  logY "sudo ln -s ln -s /usr/local/$jdk_Catalog /usr/local/jdk" #1-20#
    sudo ln -s /usr/local/$jdk_Catalog /usr/local/jdk
  checkTF #21#

  logW "4. 修改环境变量" #1-20#

    var1="export JAVA_HOME=/usr/local/jdk"
    var2='export PATH=$JAVA_HOME/bin:$PATH'
    var3='export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar'

    for i in "$var1" "$var2" "$var3"
    do
       logY "grep --quiet \"^$i$\" /etc/profile || sed -i \"\$a $i\" /etc/profile" #1-20#
       sudo grep --quiet "^$i$" /etc/profile || sudo sed -i "\$a $i" /etc/profile
       checkTF #21#
    done

  logY "java -version"
    source /etc/profile && java -version
  checkTF #21#

}

check_Version #25#

systemtype=$(uname -a | grep -Eo '(ubuntu|centos7)')

if [[ $systemtype == centos7 ]]; then 
   jdk_Install_centos7  ##1

elif [[ $systemtype == ubuntu ]]; then
     jdk_Install_ubuntu  ##2

fi
